using UnityEngine;

public class LunaController : MonoBehaviour
{
    private Rigidbody2D rigidbody2DD;
    private Animator animator;
    private Vector2 move = new Vector2(0, 0);
    private float moveScale = 0.0f;
    public float moveSpeed = 3.0f;

    
    public GameObject dialog;
    private DialogController dialogController = null;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2DD = GetComponent<Rigidbody2D>();
        animator = GetComponentInChildren<Animator>();
        dialogController = dialog.GetComponent<DialogController>();
        // 检查任务
        if(QuestSystem.Instance.CurrentQuest == null)
        {
            QuestSystem.Instance.GetQuest();
        }
        var currentQuest = QuestSystem.Instance.CurrentQuest;
        if(currentQuest is SystemQuest && currentQuest.Status == QuestStatus.Available)
        {
            // 领取系统任务
            dialogController.ShowDialog();
        }
    }

    // Update is called once per frame
    void Update()
    {
        switch (LunaStatusManager.Instance.LunaState)
        {
            case LunaState.Normal:
                /* 控制露娜 */

                var horizontal = Input.GetAxisRaw(Defines.Horizontal);
                var vertical = Input.GetAxisRaw(Defines.Vertical);

                if (!(horizontal == 0 && vertical == 0))
                {
                    /* 通过Input的值（方向）找到对应的动画 */
                    animator.SetFloat(Defines.LookX, horizontal);
                    animator.SetFloat(Defines.LookY, vertical);
                }

                move = new Vector2(horizontal, vertical);
                moveScale = move.magnitude;

                if (move.magnitude > 0)
                {
                    if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
                    {
                        moveScale = 2.0f;
                        moveSpeed = 3.5f;
                    }
                    else
                    {
                        moveScale = 1.0f;
                        moveSpeed = 2.0f;
                    }
                }

                /* 通过moveScale的值找到状态机的条件，来切换状态 */
                animator.SetFloat(Defines.MoveValue, moveScale);

                /* 回血回蓝 */
        
                if (LunaStatusManager.Instance.CurrentHP < LunaStatusManager.MaxHP)
                    LunaStatusManager.Instance.ChangeHP(Time.deltaTime);
                if (LunaStatusManager.Instance.CurrentMP < LunaStatusManager.MaxMP)
                    LunaStatusManager.Instance.ChangeMP(Time.deltaTime);
                
                break;
            case LunaState.Battle:
                break;
            case LunaState.Dialog:
                animator.SetFloat(Defines.MoveValue, 0);
                break;
        }

        /* 退出游戏 */

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    void FixedUpdate()
    {
        switch (LunaStatusManager.Instance.LunaState)
        {
            case LunaState.Normal:
                Vector2 position = transform.position;
                position += moveSpeed * move * Time.fixedDeltaTime;
                rigidbody2DD.MovePosition(position);
                break;
            case LunaState.Battle:
                break;
        }
    }

    public void Climb(bool value)
    {
        animator.SetBool(Defines.Climb, value);
    }

    public void Jump(bool value)
    {
        animator.SetBool(Defines.Jump, value);
    }
}
